// packageAPI/pages/media/camera/camera.js

var cameraContext = null;
var listener = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'',
    schema: [
      {
        label:'录制时长(s)：',
        prop:'timeout',
        type:'number',
        numRang: [30, 300]
      },
      {
        label:'开启镜像：',
        prop:'selfieMirror',
        type:'picker',
        labelOptions:['是','否'],
        valueOptions:[true, false]
      },
      {
        label:'缩放级别：',
        prop:'zoom',
        type:'number',
      },
      {
        label:'视频压缩：',
        prop:'compressed',
        type:'picker',
        labelOptions:['是','否'],
        valueOptions:[true, false]
      },
    ],
    options:{
      timeout: 30,
      selfieMirror:true,
      zoom: 1,
      compressed: false
    },
    result: '',
    frameData: '',
    pzImgUrl: '',
    videoUrl: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    cameraContext = wx.createCameraContext();
    let that = this;
    listener = cameraContext.onCameraFrame((frame) => {
      console.log(frame.data instanceof ArrayBuffer, frame.data, frame.width, frame.height)
    })
    console.log('wx.createCameraContext()', cameraContext);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 获取设置的值
  setOptons(e) {
    console.log('获取设置的值', e.detail)
    this.setData({
      options: e.detail.formData
    })
  },
  // 开始录像
  startRecordingFunc() {
    let that = this;
    cameraContext.startRecord({
      timeout: that.data.options.timeout,
      selfieMirror: that.data.options.selfieMirror,
      success(res) {
        console.log('开始录制成功', res);
        that.setData({
          result: res
        })
      },
      fail(error) {
        console.log('开始录像失败', error)
        that.setData({
          result: error
        })
      },
      complete(res) {
        console.log('接口调用结束的回调函数', res)
      },
      timeoutCallback(res) {
        console.log('超过录制时长上限', res)
        that.setData({
          result: res
        })
      }
    })
    
  },
  // 停止录制
  stopRecordingFunc() {
    let that = this;
    cameraContext.stopRecord({
      compressed: that.data.options.compressed,
      success(res) {
        console.log('停止录制成功', res)
        that.setData({
          result: res,
          videoUrl: res.tempVideoPath
        })
      },
      fail(error) {
        console.log('停止录制失败', error)
        that.setData({
          result: error
        })
      },
      complete(res) {
        console.log('停止录制继续调用', res)
      }
    })
  },
  // 设置缩放
  sizeZoomFunc() {
    let that = this;
    cameraContext.setZoom({
      zoom: Number(that.data.options.zoom),
      success(res) {
        console.log('设置缩放成功', res)
        that.setData({
          result: res
        })
      },
      fail(error) {
        console.log('设置缩放失败', error)
        that.setData({
          result: error
        })
      },
      complete(res) {
        console.log('设置缩放继续调用', res)
      }
    })
  },
  setZoomFunc(e) {
    console.log('设置缩放', e)
  },
  // 拍摄照片
  takePhotoFunc() {
    let that = this;
    cameraContext.takePhoto({
      quality: 'normal',
      selfieMirror: false,
      success(res) {
        console.log('拍摄照片成功', res)
        that.setData({
          result: res,
          pzImgUrl: res.tempImagePath
        })
      },
      fail(error) {
        console.log('拍摄照片失败', error)
        that.setData({
          result: error
        })
      },
      complete(res) {
        console.log('拍摄照片继续调用', res)
      }
    })
  },
  // 开始监听帧率
  startListenerFunc() {
    let that = this;
    listener.start({
      success(res) {
        console.log('开始监听帧率', res)
        that.setData({
          result: res
        })
      },
      fail(error) {
        console.log('开始监听帧率失败', error)
        that.setData({
          result: error
        })
      },
      complete(res) {
        console.log('开始监听帧率继续调用', res)
      }
    });
  },
  // 停止监听帧率
  stopListenerFunc() {
    let that = this;
    listener.stop({
      success(res) {
        console.log('停止监听帧率', res)
        that.setData({
          result: res
        })
      },
      fail(error) {
        console.log('停止监听帧率失败', error)
        that.setData({
          result: error
        })
      },
      complete(res) {
        console.log('停止监听帧率继续调用', res)
      }
    });
  }
})